﻿@page
@model OrganizationModel
@inject IDataService  DataService
@{
    ViewData["Title"] = "Manage Organization";
    var currentUser = DataService.GetUserByName(User.Identity.Name);
    var organizationID = currentUser.OrganizationID;
    var isAdmin = currentUser.IsAdministrator;

}
<h4>@ViewData["Title"]</h4>

@if (isAdmin)
{
    <partial name="_StatusMessage" for="StatusMessage" />

    <div asp-validation-summary="All" class="text-danger"></div>

    <div class="row">
        <div class="col-sm-8">
            @* Organization ID *@
            <div class="form-group mw">
                <label class="mb-1">Organization ID</label>
                <input readonly class="form-control" value="@organizationID" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-8">
            @* Organization Name *@
            <div class="form-group mw">
                <label class="mb-1">Organization Name</label>

                <div class="input-group">
                    <input id="organizationNameInput" asp-for="OrganizationName" placeholder="Enter a name for your organization" class="form-control" />
                    <div class="input-group-append">
                        <div class="fa fa-edit input-group-text"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-8">

            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            @* Device Groups *@
            <form asp-page-handler="CreateDeviceGroup">
                <div class="form-group">
                    <label class="mb-1">Device Groups</label>
                    <span id="deviceGroupHelpButton" class="fas fa-question-circle pointer"></span>
                    <select id="deviceGroupList" multiple asp-items="Model.DeviceGroupSelectItems" class="form-control all-device-groups-list mb-1">
                    </select>

                    <div class="text-right mb-3">
                        <button id="addUsersToDeviceGroupButton" type="button" class="btn btn-secondary">Users</button>
                        <button id="removeDeviceGroupButton" type="button" class="btn btn-secondary">Remove</button>
                    </div>

                    <div class="input-group">
                        <input id="deviceGroupInput" placeholder="Add new device group" asp-for="Input.DeviceGroupName" class="form-control" maxlength="200" />
                        <button id="addDeviceGroupButton" type="submit" class="btn btn-secondary">Add</button>
                    </div>
                </div>
            </form>

        </div>
    </div>

    <div class="row">
        <div class="col-sm-12">
            @* Users *@
            <div class="form-group">
                <label class="control-label">Users</label>
                <span id="usersHelpButton" class="fas fa-question-circle pointer"></span>
                <table class="table table-striped table-hover" style="max-height: 400px">
                    <thead>
                        <tr>
                            <th>User Name</th>
                            <th>Administrator</th>
                            <th>Reset Password</th>
                            <th>Remove</th>
                        </tr>
                    </thead>
                    <tbody>
                        @for (var i = 0; i < Model.Users.Count; i++)
                        {
                            <tr user="@Model.Users[i].ID">
                                <td class="middle-aligned"><label class="control-label">@Model.Users[i].UserName</label></td>
                                @if (currentUser.Id == Model.Users[i].ID)
                                {
                                    <td>@Html.CheckBoxFor(x => x.Users[i].IsAdmin, new { user = Model.Users[i].ID, @class = "user-is-admin-checkbox", disabled = "disabled" })</td>
                                    <td></td>
                                    <td><button type="button" class="btn btn-danger delete-user-button" user="@Model.Users[i].ID" disabled>Delete</button></td>
                                }
                                else
                                {
                                    <td>@Html.CheckBoxFor(x => x.Users[i].IsAdmin, new { user = Model.Users[i].ID, @class = "user-is-admin-checkbox" })</td>
                                    <td><button type="button" class="btn btn-danger reset-password-button" user="@Model.Users[i].ID">Reset</button></td>
                                    <td><button type="button" class="btn btn-danger delete-user-button" user="@Model.Users[i].ID">Delete</button></td>
                                }
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>


    @* Invitations *@
    <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
                <label class="control-label">Invitations</label>
                <span id="invitesHelpButton" class="fas fa-question-circle pointer"></span>
                <table id="invitesTable" class="table table-striped table-hover" style="max-height: 400px">
                    <thead>
                        <tr>
                            <th>Invited User</th>
                            <th>Administrator</th>
                            <th>Link(s)</th>
                            <th>Delete</th>
                        </tr>
                    </thead>
                    <tbody>
                        @for (var i = 0; i < Model.Invites.Count; i++)
                        {
                            <tr invite="@Model.Invites[i].ID">
                                <td class="middle-aligned"><label class="control-label">@Model.Invites[i].InvitedUser</label></td>
                                <td class="middle-aligned">@Html.CheckBoxFor(x => x.Invites[i].IsAdmin, new { disabled = "disabled" })</td>
                                <td class="middle-aligned">
                                    <label class="control-label">
                                        <a href="@Request.Scheme://@Request.Host/Invite/?id=@Model.Invites[i].ID">Join Link</a>
                                    </label>
                                </td>
                                <td><button type="button" class="btn btn-danger delete-invite-button" invite="@Model.Invites[i].ID">Delete</button></td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>


    @* Send Invites *@
    <div class="row">
        <div class="col-sm-8">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <form method="post" asp-page-handler="AddUser">
                <div class="form-group">
                    <label class="control-label">Add User</label>
                    <div class="input-group">
                        <input asp-for="Input.UserEmail" placeholder="Email of user to add" type="email" required class="form-control" />
                        <div class="input-group-append">
                            <span class="input-group-text pr-1">Admin?</span>
                        </div>
                        <div class="input-group-append">
                            <div class="input-group-text pl-1">
                                <input asp-for="Input.IsAdmin" type="checkbox" />
                            </div>
                        </div>
                        <div class="input-group-append">
                            <button type="submit" class="btn btn-secondary">Add</button>
                        </div>
                    </div>
                </div>
            </form>

        </div>
    </div>


    @* Device Permission Modals *@
    @for (var i = 0; i < Model.DeviceGroups.Count; i++)
    {
        <div group="@Model.DeviceGroups[i].ID" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Users <span class="small text-muted">for @Model.DeviceGroups[i].Name</span></h5>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-1">
                            Use this form to give users access to this device group.  Once added, only the
                            users in this list will have access.
                        </div>
                        <div class="mb-3">
                            If no users are added, everyone will have access.
                        </div>
                        <select multiple class="form-control device-group-user-list mb-1">
                            @if (Model.DeviceGroups[i]?.Users?.Any() == true)
                            {
                                foreach (var user in Model.DeviceGroups[i].Users)
                                {
                                    <option value="@user.Id">@user.UserName</option>
                                }
                            }
                        </select>
                        <div class="text-right mb-3">
                            <button type="button" class="btn btn-secondary remove-user-from-device-group-button" group="@Model.DeviceGroups[i].ID">
                                Remove
                            </button>
                        </div>

                        <div class="input-group">
                            <input placeholder="Add user to device group" class="form-control add-user-to-devicegroup-input" group="@Model.DeviceGroups[i].ID" maxlength="200" />
                            <button type="button" class="btn btn-secondary add-user-to-devicegroup-button" group="@Model.DeviceGroups[i].ID">
                                Add
                            </button>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>

    }

    <script>
        require.config({
            waitSeconds: 30,
            paths: {
                "App": "/out"
            }
        });
        require(["App"], function (remotely) {
            remotely.App.Pages.OrganizationManagement.Init();
        });
    </script>

}
else
{
    <h5 class="text-muted">Only organization administrators can view this page.</h5>
}